package com.si.anddos;

import com.si.anddos.filechooser.FileFolderChooser;

import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TimePicker;
/**
 * A class representing application settings dialog in anddos application
 * @author Carlos Martinez
 *
 */
public class SettingsActivity extends OptionsMenuActivity implements DialogInterface.OnClickListener {
	
	static final int TIME_DIALOG_ID = 0;
	
	private FileFolderChooser mFolderDialog;
	
	private Button buttonSaveClose;
	
	private EditText editTextFolder;
	
	private Spinner spinnerDate;

    private Button buttonPickDefaultTime;
    private int pickHour;
    private int pickMinute;
 
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.settings); 
            setTitle(R.string.titleSettings);
            //listener for save and close button
            buttonSaveClose = (Button) findViewById(R.id.buttonCloseSettings);
            buttonSaveClose.setOnClickListener(new View.OnClickListener() {
            	public void onClick(View view) {
                	finish();
            	}
            });
            
                        
            
            //fill date spinner with string array from resources
            spinnerDate = (Spinner) findViewById(R.id.spinnerDate);
            ArrayAdapter<CharSequence> adapterDate = ArrayAdapter.createFromResource(this, R.array.spinnerDate, android.R.layout.simple_spinner_item);
            adapterDate.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinnerDate.setAdapter(adapterDate);
            
            //listener for pick time button
            buttonPickDefaultTime = (Button) findViewById(R.id.buttonPickDefaultTime);
            buttonPickDefaultTime.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    showDialog(TIME_DIALOG_ID);
                }
            });
            
            
            editTextFolder = (EditText) findViewById(R.id.editTextFolder);
            
            //load shared settings and update spinner and text for time
            SharedPreferences settings = getSharedPreferences(Globals.PREFS_NAME, 0);
            String dataFolder = settings.getString(Globals.DATA_FOLDER, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath());
            editTextFolder.setText(dataFolder);
            pickHour = settings.getInt("defHour", 0);
            pickMinute = settings.getInt("defMinute", 0);
            int datePos = settings.getInt("date", 0);
            spinnerDate.setSelection(datePos);
            
            // display the current time
            updateTextDefaultTimeDisplay();
    }
	
	@Override
	protected void onPause() {
		super.onPause();
		
		// save settings
	    SharedPreferences settings = getSharedPreferences(Globals.PREFS_NAME, MODE_PRIVATE);
	    SharedPreferences.Editor editor = settings.edit();
	    editor.putString(Globals.DATA_FOLDER, editTextFolder.getText().toString());
	    editor.putInt("defHour", pickHour);
	    editor.putInt("defMinute", pickMinute);
	    editor.putInt("date", spinnerDate.getSelectedItemPosition());
	    // don't forget commit editor!
	    editor.commit();
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        // remove settings item from options menu created in base class OptionsMenuActivity
        menu.removeItem(Globals.SETTINGS_ID);
        return true;
    }
	
	private void updateTextDefaultTimeDisplay() {
		//updates the time we display as the text in button
		buttonPickDefaultTime.setText(new StringBuilder()
			.append(pad(pickHour))
			.append(":")
			.append(pad(pickMinute)));
	}

	private static String pad(int c) {
		// adding zeros for correct time viewing
	    if (c >= 10)
	        return String.valueOf(c);
	    else
	        return "0" + String.valueOf(c);
	}
	
	// the callback received when the user "sets" the time in the dialog
	private TimePickerDialog.OnTimeSetListener mTimeSetListener =
	    new TimePickerDialog.OnTimeSetListener() {
	        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
	            pickHour = hourOfDay;
	            pickMinute = minute;
	            updateTextDefaultTimeDisplay();
	        }
	};
	
	@Override
	protected Dialog onCreateDialog(int id) {
		// creating time picker dialog
	    switch (id) {
	    case TIME_DIALOG_ID:
	        return new TimePickerDialog(this, mTimeSetListener, pickHour, pickMinute, true);
	    }
	    return null;
	}

	public void onClick(DialogInterface dialog, int which) {
		if (dialog == mFolderDialog) {
			editTextFolder.setText(mFolderDialog.getPath());
		}
	}
	
	public void chooseFolder(View v) {
		mFolderDialog = new FileFolderChooser(this, editTextFolder.getText().toString(), this, android.R.style.Theme_Light);
		mFolderDialog.show();
	}
	
}
